package com.leruan.aasimp.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.dao.ForumUserDao;
import com.leruan.aasimp.entity.Forumuser;

public class ForumUserDaoImpl extends HibernateDaoSupport implements ForumUserDao {

    public Forumuser findBoardUserById(String boardUserId) {
        Session session = this.getSession();
        String hql = "from Forumuser where userId=?";
        return (Forumuser) session.createQuery(hql).setString(0, boardUserId).uniqueResult();
    }

    public Forumuser findForumUserByUsernameAndPassword(String username, String password) {
        Session session = this.getSession();
        String hql = "from Forumuser where username=? and password=?";
        return (Forumuser) session.createQuery(hql).setString(0, username).setString(1, password).uniqueResult();
    }

    public void insertForumUser(Forumuser forumuser) {
        Session session = this.getSession();
        session.save(forumuser);
    }

    public Forumuser findForumUserByUsername(String username) {
        Session session = this.getSession();
        String hql = "from Forumuser where username = ?";
        return (Forumuser) session.createQuery(hql).setString(0, username).uniqueResult();
    }

    public String findPassword(String username, String email) {
        Session session = this.getSession();
        StringBuffer sb = new StringBuffer("selece password from Forumuser where username = ? and email = ?");
        Query query = session.createQuery(sb.toString());
        String password = (String) query.uniqueResult();
        return password;
    }

    public List<Forumuser> findForumUser(String username, Long state, int pageNo) {
        Session session = this.getSession();

        StringBuffer sb = new StringBuffer(
                "select userId,username,nickname,state,score,userType from Forumuser where state=? ");
        if (null != username || !"".equals(username)) {
            sb.append(" and username like'%" + username + "%'");
        }
        sb.append(" order by registerDate desc");
        Query query = session.createQuery(sb.toString());
        int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE;
        query.setLong(0, state);
        query.setFirstResult(first);
        query.setMaxResults(UsedCarConstant.PAGE_SIZE);

        List list = query.list();
        List<Forumuser> userList = new ArrayList<Forumuser>();
        for (int i = 0; i < list.size(); i++) {
            Forumuser forumuser = new Forumuser();
            Object[] object = (Object[]) list.get(i);
            forumuser.setUserId((String) object[0]);
            forumuser.setUsername((String) object[1]);
            forumuser.setNickname((String) object[2]);
            forumuser.setState((Long) object[3]);
            forumuser.setScore((Long) object[4]);
            forumuser.setUserType((Long) object[5]);
            userList.add(forumuser);
        }
        return userList;
    }

    public Long findForumUserTotal(String username, Long state) {
        Session session = this.getSession();
        StringBuffer sb = new StringBuffer("select count(id) from Forumuser where state=?");
        if (null != username || !"".equals(username)) {
            sb.append(" and username like'%" + username + "%'");
        }
        sb.append("order by registerDate desc");
        Query query = session.createQuery(sb.toString());
        query.setLong(0, state);
        return (Long) query.uniqueResult();
    }

    public void deleteUser(Forumuser forumuser) {
        Session session = this.getSession();
        String hql = "delete from Forumuser where userId=?";
        session.createQuery(hql).setString(0, forumuser.getUserId()).executeUpdate();
    }

	public void updateForunUser(Forumuser user) {
		Session session = this.getSession();
		session.save(user);
	}
}
